package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * deviceController
 *
 * @author ruoyi
 * @date 2025-10-23
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "device相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;


    /**
     * 删除device
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "device", businessType = BusinessType.DELETE)
    @DeleteMapping
    @ApiOperation("删除device")
    public AjaxResult remove(@RequestBody DeviceDto dto) {
        deviceService.deleteDevice(dto);
        return success();
    }

    /**
     * 修改设备备注名称
     */
    @PutMapping
    public AjaxResult updateDevice(@RequestBody DeviceDto dto) {
        deviceService.updateDeviceBrDeviceId(dto);
        return success();

    }

    /**
     * 查询指定设备的物理模型运行状态
     */
    @PostMapping("/queryDevicePropertyStatus")
    public AjaxResult queryDevicePropertyStatus(@RequestBody DeviceDto dto) {
        Map<String, Object> map = deviceService.queryDevicePropertyStatus(dto.getDeviceName(), dto.getProductKey());
        return success(map);
    }


    /**
     * 查询已发布物模型
     */
    @PostMapping("/queryProductModelInfo")
    public AjaxResult queryProductModelInfo(@RequestBody DeviceDto dto) {
        return success(deviceService.queryProductModelInfo(dto.getProductKey()));
    }

    /**
     * 查询设备详细信息
     */
    @PostMapping("/queryDeviceDetail")
    public AjaxResult queryDeviceDetail(@RequestBody DeviceDto dto) {
        DeviceDetailVo vo = deviceService.queryDeviceDetail(dto.getIotId());
        return success(vo);
    }


    /**
     * 新增设备
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return success();
    }

    /**
     * 从物联网同步产品列表
     */

    @PostMapping("/syncProductList")
    @ApiOperation("从物联网同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     */
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品列表")
    public AjaxResult allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return success(list);
    }

    /**
     * 查询device列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取device列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出device列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "device", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出device列表")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "device数据");
    }

    /**
     * 获取device详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取device详细信息")
    public R<Device> getInfo(@ApiParam(value = "deviceID", required = true)
                             @PathVariable("id") Long id) {
        return R.ok(deviceService.selectDeviceById(id));
    }
}
